package com.shortlink.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.shortlink.mapper.LinkMapper;
import com.shortlink.pojos.dto.dataobject.ResponseResult;
import com.shortlink.pojos.dto.dataobject.ShortLinkStatsRecordDO;
import com.shortlink.pojos.entity.LinkDO;
import com.shortlink.common.properties.JwtProperties;
import com.shortlink.common.properties.LinkProperties;

import com.shortlink.utils.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/demo")
@Slf4j
@RequiredArgsConstructor
public class DemoController {

    private final JwtProperties jwtProperties;
    private final LinkProperties linkProperties;
    private final StringRedisTemplate stringRedisTemplate;
    private final LinkMapper linkMapper;

    /**
     * 测试properties
     * @return
     */
    @GetMapping("/connect")
    public String connect() {
        String str = new StringBuilder().append("link host:" + linkProperties.getHost() + "\n")
                .append("link port:" + linkProperties.getPort() + "\n")
                .append("link maxNum:" + linkProperties.getMaxNum() + "\n")
                .append("jwt:" + jwtProperties.getName() + "\n").toString();
        return str;
    }

    @GetMapping("/database")
    public ResponseResult<List<LinkDO>> database(){
        LinkDO isNull = linkMapper.selectOne(new LambdaQueryWrapper<>(LinkDO.class)
                .eq(LinkDO::getId, 1));
        LinkDO linkDO = new LinkDO();
        linkDO.setId(1L);
        linkDO.setShortUri("short");
        linkDO.setOriginUrl("origin");
        linkDO.setGid("1");
        linkDO.setEnableStatus("0");
        linkDO.setValidDateType("0");
        linkDO.setDelFlag("0");

        if(isNull==null) linkMapper.insert(linkDO);
        List<LinkDO> linkDOS = linkMapper.selectList(new LambdaQueryWrapper<>(LinkDO.class));
        return ResponseResult.okResult(linkDOS);
    }

    @GetMapping("/date")
    public ResponseResult<List<Date>> date(){
        Date now=DateTimeUtils.getNow();
        Date yesterdayNow=DateTimeUtils.getYesterdayNow();
        Date today=DateTimeUtils.getToday();
        List<Date> weekDates = DateTimeUtils.getWeek();

        List<Date> res=new ArrayList<>();
        res.add(now);
        res.add(yesterdayNow);
        res.add(today);
        weekDates.forEach(each ->{
            res.add(each);
        });


        return ResponseResult.okResult(res);
    }

    @GetMapping("sqldate")
    public ResponseResult<LinkDO> sqldate(){
        LinkDO linkDO = new LinkDO();
        linkDO.setShortUri("short");
        linkDO.setOriginUrl("origin");
        linkDO.setGid("gid");
        linkDO.setEnableStatus("0");
        linkDO.setDelFlag("0");
        linkDO.setValidDateType("0");
        linkMapper.insert(linkDO);

        return ResponseResult.okResult(linkDO);
    }

    @GetMapping("/serialize")
    private ResponseResult<ShortLinkStatsRecordDO> serialize(){
        String json="\"{\\\"browser\\\":\\\"Microsoft Edge\\\",\\\"currentDate\\\":1731750696193,\\\"device\\\":\\\"PC\\\",\\\"ip\\\":\\\"0:0:0:0:0:0:0:1\\\",\\\"network\\\":\\\"Mobile\\\",\\\"os\\\":\\\"Windows\\\",\\\"shortUri\\\":\\\"1apIao\\\",\\\"today\\\":1731686400000,\\\"uipFirstFlag\\\":false,\\\"uvFirstFlag\\\":false}\"";
        String cleanedJson = json.substring(1, json.length() - 1).replace("\\\"", "\"");
        ShortLinkStatsRecordDO statsRecord = JSON.parseObject(cleanedJson, ShortLinkStatsRecordDO.class);

        return ResponseResult.okResult(statsRecord);
    }

    @GetMapping("/lua")
    private ResponseResult<Long> lua(){
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/test.lua")));
        redisScript.setResultType(Long.class);
        Long result = null;
        try {
            result = stringRedisTemplate.execute(redisScript,Lists.newArrayList("key"), "1");
        }
        catch (Throwable throwable){
            System.out.println("");
        }
        return ResponseResult.okResult(result);
    }



}
